include metasprites2.s
include bytecode_asm.s

;bcsetmask:
; bytecode version
;  .db <twolit, >PPU_MASK, <PPU_MASK
;  .db <callasm, >(ex_store-1), <(ex_store-1)
;  .db <exit

; -----------------------------------------
; these don't seem to work
dcompare: ; n16 n16 -- result16
	tsx
	sec
	sbc $102,x
	sta $102,x
	pla
	sbc $102,x
	sta $102,x
	pla
	vmnext

dless:   ; n16 n16 -- flag
	mcallasm dcompare ; result16
	.db <droplit, $7f, <ugreater, <exit

dgreater:   ; n16 n16 -- flag
	mcallasm dcompare ; result16
	.db <droplit, $80, <uless, <exit
; -----------------------------------------


cloud:
mif +then
		mcallasm xfetch
		.db <twolit, $00, $03, <dplus
		.db <over, <lit, $0a, <greater
mif ++then
			.db <twodrop, <twolit, $ff,0
++then
		mcallasm xstore
		mcallasm yfetch
		.db <twolit, $ff, $f8, <dplus
		.db <over, <lit, $ff, <less
mif ++then
			.db <twodrop, <twolit, $08,$80
++then
		mcallasm ystore
+then
;	mcallasm xfetch
;	mcallasm f2s
;	mcallasm yfetch
;	mcallasm f2s
	mcallasm screenxy   ; surprisingly only slightly faster than the above.
	mdouble ms_cloud_0
	mcallasm metaspr
	.db <exit

starting_obj_positions:
	.db 8,8,1,3,5,4


align 4
water_cycle_data:
	.db $2c, $1c, $3c, $30
	.db $1c, $3c, $30, $2c
	.db $3c, $30, $2c, $21
	.db $30, $2c, $1c, $3c

waterpalofs:
	pha
	lda FRAMECNT
	cmp $80
	ror a
	cmp $80
	ror a
	cmp $80
	ror a
	and #3
	asl a
	asl a
	vmnext

water_color_cycle:
	mdouble water_cycle_data
	mcallasm waterpalofs
	.db <plus, <astore
	.db <lit, 5
	mcallasm updpal
	.db <exit

; probably just going to go with one or two clouds and when they go off the top of the screen,
;  relocate them below water (avoiding flowers?)
doclouds:
	.db <lit, OAM_PRIO, <lit, OAM_ATTR_FLAGS, <store
	mcallasm everyother
	.db <dup
	mcall cloud
	.db <oplus
	.db <lit, 1, <xor
	mcall cloud
	.db <oplus
	.db <zero, <lit, OAM_ATTR_FLAGS, <store
	.db <exit



do_player_movement:

	mcallasm andcase
	.db PAD_LEFT
mif +then
	mcallasm xfetch
	mdouble $FFF0
	.db <dplus
	mcallasm xstore
	.db <exit
+then

	mcallasm andcase
	.db PAD_RIGHT
mif +then
	mcallasm xfetch
	mdouble $0010
	.db <dplus
	mcallasm xstore
	.db <exit
+then

	mcallasm andcase
	.db PAD_UP
mif +then
	mcallasm yfetch
	mdouble $FFF0
	.db <dplus
	mcallasm ystore
	.db <exit
+then

	mcallasm andcase
	.db PAD_DOWN
mif +then
	mcallasm yfetch
	mdouble $0010
	.db <dplus
	mcallasm ystore
+then
	.db <exit

include map2.s

doplayer:
	.db <zero                     ; push a zero
	mcallasm padstate
	mcall do_player_movement
	mcallasm objcolrow
	mcallasm amaploc
	.db <droptrue, <storeplus
	mcallasm screenxy
	mdouble ms_player_idle_right
	mcallasm metaspr
	.db <oplus                    ; go to next object
	.db <exit

script_frame:
	jsr bytecode
	.db <zero, <ostore
	;waste some time copying 64 bytes of RAM
;	.db <twolit, 0,0, <astore, <twolit, $03,$00, <lit, 64, <callasm, >(move-1), <(move-1)

	mcall doplayer
	mcall doclouds
	mcall water_color_cycle
	
	; test bank switch
	.db <lit,1
	mcallasm bankswitch
	
	.db <end



ppuwrite:  ; A=src ... desth/l counth/l --
	.db <lit, TEMP0, <store
	.db <lit, TEMP1, <store
	mcallasm ppuaddress
	.db <lit, TEMP0, <fetch
	.db <lit, TEMP1, <fetch
	.db <dup
mif +then
-for .db <for
		.db <dup
--for .db <for
			.db <fetchplus
			mcallasm ppustore
mnext --for
mnext -for
	.db <drop,<exit
+then
	.db <drop
--for .db <for
		.db <fetchplus
		mcallasm ppustore
mnext --for
	.db <exit


script_init:
	jsr bytecode
	mdouble starting_obj_positions
	.db <astore
	.db <zero, <ostore
	.db <lit, 3

-for .db <for
		.db <fetchplus, <zero
		mcallasm xstore
		.db <fetchplus, <zero
		mcallasm ystore
		.db <oplus
mnext -for

	; copy chr data to chr ram
	mdouble chrspr
	.db <astore
	mdouble $0000
	mdouble $1000
	mcall ppuwrite

	mdouble chrbg
	.db <astore
	mdouble $1000
	.db <twodup
	mcall ppuwrite


	.db <end